/*
 * Copyright (c) 2009-2020 Weasis Team and other contributors.
 *
 * This program and the accompanying materials are made available under the terms of the Eclipse
 * Public License 2.0 which is available at https://www.eclipse.org/legal/epl-2.0, or the Apache
 * License, Version 2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
 */
package org.weasis.base.explorer;

import java.util.Hashtable;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Deactivate;
import org.weasis.core.api.explorer.DataExplorerView;
import org.weasis.core.api.explorer.DataExplorerViewFactory;

@org.osgi.service.component.annotations.Component(service = DataExplorerViewFactory.class)
public class DefaultExplorerFactory implements DataExplorerViewFactory {

  private DefaultExplorer explorer = null;
  private FileTreeModel model = null;

  @Override
  public DataExplorerView createDataExplorerView(Hashtable<String, Object> properties) {
    if (model == null) {
      model = JIUtility.createTreeModel();
    }
    if (explorer == null) {
      JIThumbnailCache thumbCache = new JIThumbnailCache();
      explorer = new DefaultExplorer(model, thumbCache);
      explorer.iniLastPath();
    }
    return explorer;
  }

  // ================================================================================
  // OSGI service implementation
  // ================================================================================

  @Activate
  protected void activate(ComponentContext context) {
    if (model == null) {
      model = JIUtility.createTreeModel();
    }
  }

  @Deactivate
  protected void deactivate(ComponentContext context) {
    if (explorer != null) {
      explorer.saveLastPath();
    }
  }
}
